//
//  AppDelegate.m
//  01-UIView、UIWindow
//
//  Created by QC.L on 16/7/14.
//  Copyright © 2016年 QC.L. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // 程序结构的组成
    // 1.main.m主要实现了程序的正常运行
    // 2.APPDelegate程序的执行者, 签订了UIApplicationDelegate(所有的协议方法都是可选的)
    // 注意: APPDelegate就是一个普通的类, 只不过签订了协议, 才有了这么多的方法
    // 3.ViewController 视图控制器, 主要负责视图管理
    // 4.main.storyboard(视图管理)和LaunchScreen.storyboard(负责启动页, Xcode 7以前叫LaunchScreen.xib) 可视化管理
    // 5.Assets.xcassets 主要用来管理图片素材(Xcode 7以前叫images.scassets)
    // Info.plist(工程配置文件)
    // 模拟器
    // command + 1~5 改变模拟器的大小
    // command + ←/→ 横竖屏切换
#pragma mark - UIWindow
    // 程序中只有一个主Window
    // 1> 取消程序的可视化入口 main.storyboard
    // 2> 创建主Window, 使其大小与屏幕一样
    // UIscreen 系统的屏幕类
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    // 给创建的窗口填颜色 UIColor(系统的颜色类)
    _window.backgroundColor = [UIColor whiteColor];
    // 让Window成为主窗口并显示
    [_window makeKeyAndVisible];
    // Xcode 7以后, 必须设置rootViewController, 否则在改方法结束时会崩溃
    // 设置Window的主窗口
    _window.rootViewController = [[ViewController alloc] init];
    // 3> UIWindow 继承于 UIView
    // UIView(与NSObject相似, 是UI阶段的根类, 大部分UI控件继承于UIView)
#if 0
#pragma mark - 学习新类
    // 1> 需要知道新类包含哪些初始方法
    // 2> 包含哪些行为和特征(即方法和属性)
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    // 设置背景颜色
    view.backgroundColor = [UIColor redColor];
    // 添加到Window上
    [_window addSubview:view];
    // 遇到UIView的子类, 都可以用以上方式创建显示
    // UIView在屏幕中显示为矩形
#pragma mark - iOS系统坐标系
    // 与数学中的坐标系不同, y轴正方向向下, 原点是屏幕的左上角
    // frame, bounds, center
    // 1.frame(x, y, width, height) 改变的是视图在其父视图上的位置
    // 2.bounds(x, y, width, height) 改变的是其本身的坐标系, 其子视图的原点改变
    // bounds width, height修改的是本身大小, 会以中心点收缩/扩张
    // bounds的改变不会改变该视图的位置
    // 3.center 中心点 (影响视图的位置)
    // view.frame = CGRectMake(0, 0, 100, 100);
     view.center = CGPointMake(200, 200);
//    NSLog(@"bounds改变前的frame:%@", NSStringFromCGRect(view.frame));
//    NSLog(@"bounds改变前的center:%@", NSStringFromCGPoint(view.center));
    view.bounds = CGRectMake(0, 0, 300, 300);
//    NSLog(@"bounds改变后的frame:%@", NSStringFromCGRect(view.frame));
//    NSLog(@"bounds改变前的center:%@", NSStringFromCGPoint(view.center));
    UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    subView.backgroundColor = [UIColor yellowColor];
    [view addSubview:subView];
    // 改变view的大小的宽为100
//    view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y, 100, view.frame.size.height);
    // view.frame get方法
    // fram.size 结构体
    // OC和C的内容结合时, 是一个常量
//    view.frame.size.width = 100;/**< 这是OC和C的混编 */
//    CGRect rect = view.frame;
//    rect.size.width = 100;/**< 这是一个结构体(C语言) */
//    view.frame = rect;
    view.gx_width = 200;
#endif
#pragma mark - UIView 基本属性
    UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    // 添加
    [_window addSubview:testView];
    // 移除
    // [_window removeFromSuperview];
    // 1.背景颜色
    testView.backgroundColor = [UIColor redColor];
    // 2.透明度(0.0~1.0)默认值是1.0
    testView.alpha = 0.3;
    // 3.tag 标记(编号)
    testView.tag = 666;
    // 根据tag获取到对应的view __kindof
    UIView *view1 = [_window viewWithTag:666];
    // 4.subViews 子视图们
    UIView *arrayView = [_window.subviews objectAtIndex:0];
    NSLog(@"%@", arrayView);
    // 5.superView 父视图
    // testView.superview;
    // 6.hidden 隐藏/显示 YES/NO
    testView.hidden = NO;
#pragma mark - 视图层级
    UIView *firstView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
    UIView *seconView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 200, 200)];
    UIView *thirdView = [[UIView alloc] initWithFrame:CGRectMake(80, 80, 200, 200)];
    // addSubview(添加视图)
    [_window addSubview:firstView];
    // insertSubview:atIndex:(插入视图, 到指定下标)
    NSLog(@"%@", _window.subviews);
    [_window insertSubview:seconView atIndex:1];
    // insertSubview:aboveSubview:(插入视图, 到指定视图的上面)
    [_window insertSubview:thirdView aboveSubview:seconView];
    // insertSubview:belowSubview:(插入视图, 到指定视图的下面)
    [_window insertSubview:thirdView belowSubview:seconView];
    firstView.backgroundColor = [UIColor cyanColor];
    seconView.backgroundColor = [UIColor greenColor];
    thirdView.backgroundColor = [UIColor redColor];
#pragma mark - UILable(负责显示文本)
    // 如果类没有自己的初始化方法, 则使用父类的
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 50, 300, 50)];
    label.backgroundColor = [UIColor yellowColor];
    [_window addSubview:label];
    // label 的属性
    // 1.text文本
    label.text = @"皇儿是的说废话是第三代开发大上开发大书法家第三方王id挥洒了食品的烦恼扣三分";
    // 2.textColor文本颜色
    label.textColor = [UIColor redColor];
    // 3.font
    label.font = [UIFont systemFontOfSize:30];
    // 文本加粗
    label.font = [UIFont boldSystemFontOfSize:15];
    // 4.textAlignment 对齐方式, 默认是左对齐
    label.textAlignment = NSTextAlignmentCenter;
    // 5.numberOfLines 默认为1行
    // 不确定行数时, 给0
    label.numberOfLines = 2;
    // newLabel
    UILabel *newLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 500, 200, 50)];
    newLabel.backgroundColor = [UIColor redColor];
    [_window addSubview:newLabel];
    newLabel.text = @"这就是label";
    newLabel.textColor = [UIColor yellowColor];
    newLabel.textAlignment = NSTextAlignmentCenter;
    newLabel.font = [UIFont boldSystemFontOfSize:25];
    // 6.换行模式
    // NSLineBreakByWordWrapping 以单词换行
    // NSLineBreakByCharWrapping 以字符换行
    // NSLineBreakByClipping
    // NSLineBreakByTruncatingHead 内容过多时省略开头
    // NSLineBreakByTruncatingTail 内容过多时省略末尾, 默认是在末尾省略
    // NSLineBreakByTruncatingMiddle 内容过多时省略中间
    // 7.shadowColor 阴影
    newLabel.shadowColor = [UIColor blackColor];
    // shadowOffset 阴影偏移
    newLabel.shadowOffset = CGSizeMake(2, 1);
#pragma mark - UIButton
    // UIButton由两个UIImageView和一个UILabel构成
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.backgroundColor = [UIColor purpleColor];
    button.frame = CGRectMake(100, 300, 60, 30);
    // 给Button添加标题
    [button setTitle:@"你妹" forState:UIControlStateNormal];
    // 修改Button的文字颜色
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_window addSubview:button];
    // 给Button添加事件
    [button addTarget:self action:@selector(handleButton:) forControlEvents:UIControlEventTouchUpInside];
#pragma mark - UITextField
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 350, 150, 50)];
    textField.backgroundColor = [UIColor redColor];/**< 该属性默认是nil */
    // 1.text 给输入框赋初值
    // textField.text = @"hhhhhhh";
    // 2.textColor 初值文字的颜色
    textField.textColor = [UIColor yellowColor];
    // 3.UITextBorderStyleLine边框有边缘线
    // textField.borderStyle = UITextBorderStyleLine;
    // 4.UITextBorderStyleRoundedRect边缘带圆角
    textField.borderStyle = UITextBorderStyleRoundedRect;
    // 5.placeholder占位符
    // 当textField不为空(.text属性有值)时, placeHolder是不显示的
     textField.placeholder = @"这是牛逼的文本框";
    // 6.clearsOnBeginEditing开始编辑时清空文本框的输入(输错密码时)
    textField.clearsOnBeginEditing = YES;
    // 7.secureTExtEntry 密文输入(输密码)
    textField.secureTextEntry = YES;
    // 8.leftView/rightView 左右视图
    // 9.leftViewMode
    // command K 显示/收起模拟器的键盘
    [_window addSubview:textField];
    textField.delegate = self;
    return YES;
}

- (void)handleButton:(UIButton *)button {
    NSLog(@"结束编辑了");
    // 结束编辑
    [_window endEditing:YES];
}
#pragma mark - UITextFieldDelegete
// 是否允许编辑
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    return YES;
}
// 编辑时
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    
}
// 处理点击return的时间点
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    // 回收键盘
    [textField endEditing:YES];
    return YES;
}
// 已经结束编辑
- (void)textFieldDidEndEditing:(UITextField *)textField {
    
}

- (BOOL)textFieldShouldClear:(UITextField *)textField {
    return YES;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
